﻿using System.Windows.Forms;
using SdmxMl.Structure;
using SdmxMl.Manager;
using SdmxMl.Common;
using System.Xml;
using System;


namespace SdmxMl.UC
{
    public partial class UcMetadataflow : UserControl
    {
        protected TreeNode treeNode;
        private MetadataFlowInsider _metadataflowInsider;
        private ArtefactList alMsdRefList = null;
        public UcMetadataflow()
        {
            InitializeComponent();
            ucEditReference1.AtrefactType = SdmxArtefactType.MetadataStructureDefinitions;

        }

        public virtual void BindFromNode(TreeNode node)
        {
            treeNode = node;
            _metadataflowInsider = null;
            if (node != null)
            {
                _metadataflowInsider = node.Tag as MetadataFlowInsider;

            }
            BindToControls();

        }

        private void BindToControls()
        {
            MetadataStructureDefinitionType msd = null;
            if (_metadataflowInsider != null)
            {
                Visible = true;
               // tabControlRef.SelectedIndex = 0;

                ucEditReference1.ArtefactRef = _metadataflowInsider.MetadataStructureRef;
                
                alMsdRefList = ReferenceManager.GetArtefactList(SdmxArtefactType.MetadataStructureDefinitions);
                msd = ReferenceManager.LocateMetadataStructureDefinition(_metadataflowInsider.MetadataStructureRef);
                textBoxUrn.Text = _metadataflowInsider.MetadataStructureRef.Urn;


            }
            else
                Visible = false;
        }

        public bool UpdateNodeTag(TreeNode node)
        {
            bool err = false;
            if (node != null)
            {
                err = BindFromControls();
                if (_metadataflowInsider != null && node.Text != _metadataflowInsider.ToString())
                    node.Text = _metadataflowInsider.ToString();
            }

            return err; // true on error
        }

        protected bool BindFromControls()
        {
            bool err = false;
            if (_metadataflowInsider != null)
            {
                _metadataflowInsider.MetadataStructureRef.Urn = textBoxUrn.Text;
            }

            return err;
        }

        private void buttonNavigateScheme_Click(object sender, System.EventArgs e)
        {
            if (_metadataflowInsider != null)
            {
                if (_metadataflowInsider.MetadataStructureRef != null && _metadataflowInsider.MetadataStructureRef.IsFilled)
                {

                    try
                    {
                        using (new SdmxMl.Helper.CWaitCursor())
                        {
                            // If URN try to get it if not already in memory
                            ArtefactRefTyped art = new ArtefactRefTyped(
                                SdmxArtefactType.MetadataStructureDefinitions,
                                _metadataflowInsider.MetadataStructureRef.FullIdent);

                            // If already in memory, navigate otherwhile get from regidtry
                            if (NavigationManager.LocatenavigateProject(art.Type, art.FullIdent, null) == null)
                                NavigationManager.LocateNavigateWithRegistry(art.Type, art.FullIdent, true, true, null);
                        }
                    }

                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Error");
                    }
                }
            }
        }

    }
}
